package com.dhcc.bpm.modules.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.config.TemplateConfig;
import com.dhcc.bpm.common.constant.FilePathConstant;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.DateUtils;
import com.dhcc.bpm.common.utils.ReadTextFile;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.common.utils.XmlUtils;
import com.dhcc.bpm.modules.api.entity.Policy;
import com.dhcc.bpm.modules.api.entity.PolicyItem;
import com.dhcc.bpm.modules.api.entity.PolicyItemMetadata;
import com.dhcc.bpm.modules.api.entity.PolicyTag;
import com.dhcc.bpm.modules.api.service.*;
import com.dhcc.bpm.modules.api.vo.ApiListVo;
import com.dhcc.bpm.modules.api.vo.PolicyItemVO;
import com.dhcc.bpm.modules.api.vo.PolicyTagVO;
import com.dhcc.bpm.modules.api.vo.PolicyVO;
import com.dhcc.bpm.modules.common.entity.Plupload;
import com.dhcc.bpm.modules.common.entity.Upload;
import com.dhcc.bpm.modules.common.service.IPluploadService;
import com.dhcc.bpm.modules.log.entity.ApiOperLog;
import com.dhcc.bpm.modules.log.service.IApiOperLogService;
import com.dhcc.bpm.modules.metadata.entity.ApiMetadata;
import com.dhcc.bpm.modules.metadata.service.IApiMetadataService;
import com.dhcc.bpm.modules.metadata.vo.ApiMetadataVO;
import com.dhcc.cip.sss.policy.MessageMain;
import com.dhcc.cip.sss.policy.common.enums.ConversionType;
import com.dhcc.cip.sss.policy.common.enums.MessageType;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.tool.api.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @Title
 * @ClassName PolicyController
 * @Desription
 * @Author zhangzexu
 * @Date 2020/10/9 14:15
 * @Version V1.0
 */
@Slf4j
@RestController
@RequestMapping("bpm-policy/policyapi")
@Api(value = "api发布表", tags = "api发布表接口")
public class PolicyApiController extends BladeController {

    @Autowired
    IApiOperLogService apiOperLogService;
    @Autowired
    IPolicyService policyService;
    @Autowired
    IPolicyTagService policyTagService;
    @Autowired
    IPolicyItemService policyItemService;
    @Autowired
    private IPluploadService pluploadService;

    @Autowired
    IPolicyItemMetadataService policyItemMetadataService;

    @Autowired
    IApiMetadataService apiMetadataService;

    @Autowired
    IApiService apiService;

    @Value("${bpm.file-server-path}")
    private String fileServerPath;

    private String fileUploadPath;

    @Value("${bpm.file-public-path}")
    public void setFileUploadPath(String publicPath) {
        fileUploadPath = publicPath;
    }

    private List<String> supportExcelType = Lists.newArrayList( "xml");

    @ApiLog("报文配置文件导入")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "报文配置文件导入", notes = "报文配置文件导入")
    @Log(title = "报文配置文件导入", businessType = BusinessType.SELECT)
    @PostMapping("importPolicyXml")
    public R importexcel(Plupload plupload, HttpServletRequest request) {
        Long id = Long.parseLong(request.getParameter("apiId"));
        String backserviceReqType = request.getParameter("backserviceReqType");
        Upload upload = new Upload();
        upload.setRequest(request);
        Upload uploadEntity = pluploadService.getFile(upload);
        MultipartFile file = uploadEntity.getMultipartFile();
        String name = file.getOriginalFilename();
        String fileType = name.substring(name.lastIndexOf(".") + 1);
        if(!supportExcelType.contains(fileType)){
            return R.fail("请上传xml类型文档！");
        }
        uploadEntity.setName(name);
        com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(id);
        com.dhcc.bpm.modules.api.entity.Api oldParam = api;
        upload.setApiName(api.getApiCode());
        //本地上传
        Long fileId = pluploadService.fileLocalUpload(plupload, upload, request);

        api.setMsgConfigId(fileId);
        api.setBackserviceReqType(backserviceReqType);
        api.setIsUploadBackFile("1");
        apiService.updateById(api);
        saveApiOpeLog(StringUtils.toJsonString(oldParam), StringUtils.toJsonString(api), api.getApiVersion(), api.getId());
        return R.data(id);
    }

    @ApiLog("报文配置文件导入")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "报文配置文件导入", notes = "报文配置文件导入")
    @Log(title = "报文配置文件导入", businessType = BusinessType.SELECT)
    @PostMapping("addPolicyXml")
    public R addPolicyXml(@RequestBody JSONObject object) {
        Long id = object.getLong("apiId");
        Long msgConfigId = object.getLong("msgConfigId");
        String backserviceReqType = object.getString("backserviceReqType");
        String isCommonConfig = object.getString("isCommonConfig");

        com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(id);
        api.setIsCommonConfig(isCommonConfig);
        api.setMsgConfigId(msgConfigId);
        api.setBackserviceReqType(backserviceReqType);
        api.setIsUploadBackFile("1");
        apiService.updateById(api);

        return R.data(id);
    }

    @ApiLog("报文配置文件读取")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "报文配置文件读取", notes = "报文配置文件读取")
    @Log(title = "报文配置文件读取", businessType = BusinessType.SELECT)
    @PostMapping("readPolicyXml")
    public R readPolicyXml(@RequestBody JSONObject object) {
        Long id = object.getLong("id");
        if (id == null) {
            return R.data(null);
        }
        com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(id);
        if (null == api.getMsgConfigId()) {
            return R.data("");
        }
        Plupload plupload = pluploadService.selectById(api.getMsgConfigId());
        if (plupload == null) {
            return R.data("");
        }

        StringBuilder string = new StringBuilder();
        //xml 格式化
        try {
            String path = plupload.getFileFullpath().replace(FilePathConstant.FILEPROXY, fileUploadPath);
            log.info("报文配置路径：{}", path);
            File file = new File(path);
            if (!file.exists()) {
                return R.data("");
            }
            try {
                FileReader reader = new FileReader(path);
                BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
                String line;
                //网友推荐更加简洁的写法
                while ((line = br.readLine()) != null) {
                    // 一次读入一行数据
                    string.append(line);
                    string.append("\n");
                }
            } catch (IOException e) {
                log.error("文件读取失败", e);
            }
//            if (file.exists()) {
//                file.delete();
//            }

//            RandomAccessFile randomAccessFile = new RandomAccessFile(plupload.getFileFullpath(), "rw");
//            randomAccessFile.close();
//            randomAccessFile = new RandomAccessFile(plupload.getFileFullpath(), "rw");
//            randomAccessFile.seek(0);
//            String msg = XmlUtils.format(string.toString());
//            randomAccessFile.write(msg.getBytes("GBK"));

            JSONObject json = new JSONObject();
            json.put("message", string);
            json.put("fileName", plupload.getFileRealname());
            return R.data(json);
        } catch (Exception e) {
            log.error("生成报文拆组包失败", e);
            return R.fail(e.getLocalizedMessage());
        }

    }

    @ApiLog("报文配置文件读取")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "报文配置文件读取", notes = "报文配置文件读取")
    @Log(title = "报文配置文件读取", businessType = BusinessType.SELECT)
    @PostMapping("readPolicyXmlByXmlId")
    public R readPolicyXmlByXmlId(@RequestBody JSONObject object) {
        Long id = object.getLong("id");
        if (id == null) {
            return R.data(null);
        }
        Plupload plupload = pluploadService.selectById(id);
        if (plupload == null) {
            return R.data("");
        }

        StringBuilder string = new StringBuilder();
        //xml 格式化
        try {
            String path = plupload.getFileFullpath().replace(FilePathConstant.FILEPROXY, fileUploadPath);
            log.info("报文配置路径：{}", path);
            File file = new File(path);
            if (!file.exists()) {
                return R.data("");
            }
            try {
                FileReader reader = new FileReader(path);
                BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
                String line;
                //网友推荐更加简洁的写法
                while ((line = br.readLine()) != null) {
                    // 一次读入一行数据
                    string.append(line);
                    string.append("\n");
                }
            } catch (IOException e) {
                log.error("文件读取失败", e);
            }

            JSONObject json = new JSONObject();
            json.put("message", string);
            json.put("fileName", plupload.getFileRealname());
            return R.data(json);
        } catch (Exception e) {
            log.error("生成报文拆组包失败", e);
            return R.fail(e.getLocalizedMessage());
        }

    }

    /**
     * @param policyVO
     * @return org.springblade.core.tool.api.R
     * @Description 创建报文配置
     * @Author zhangzexu
     * @Date 14:39 2020/10/09
     **/
    @PostMapping("/createPolicy")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "创建报文配置", notes = "传入policyVO")
    @Log(title = "创建报文配置", businessType = BusinessType.INSERT)
    @ApiLog("创建报文配置")
    public R createPolicy(@RequestBody PolicyVO policyVO) {
        try {
            Policy policy = policyService.getOne(new QueryWrapper<Policy>().eq("api_id", policyVO.getApiId()));
            PolicyTag policyTag = new PolicyTag();

            Policy policy1 = new Policy();
            BeanUtils.copyProperties(policyVO, policy1);
            BeanUtils.copyProperties(policyVO, policyTag);
            Long policyId;
            if (policy != null) {
                policyId = policy.getId();
                policy1.setId(policy.getId());
                policyService.updateById(policy1);
            } else {
                policyService.save(policy1);
                policyId = policyService.getOne(new QueryWrapper<Policy>().eq("api_id", policy1.getApiId())).getId();
            }

            if (policyTag != null) {
                policyTag.setPolicyId(policyId);
                policyTagService.save(policyTag);
            }
        } catch (Exception e) {
            log.error("报文创建出错", e);
            return R.fail("报文创建出错" + e.getMessage());
        }
        return R.success("添加成功");

    }


    /**
     * @param apiId
     * @return org.springblade.core.tool.api.R
     * @Description 查询报文基本信息
     * @Author zhangzexu
     * @Date 14:39 2020/10/09
     **/
    @GetMapping("/selectPolicyInfo")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询报文基本信息", notes = "传入apiId")
    @Log(title = "查询报文基本信息", businessType = BusinessType.SELECT)
    @ApiLog("查询报文基本信息")
    public R selectPolicyInfo(@RequestParam String apiId) {
        Policy policy = policyService.getOne(new QueryWrapper<Policy>().eq("api_id", apiId));
        JSONObject object = new JSONObject();
        if (policy == null) {
            return R.success("");
        } else {
            object.put("policy", policy);
            List<PolicyTag> policyTagList = policyTagService.list(new QueryWrapper<PolicyTag>().eq("POLICY_ID", policy.getId()));
            List<PolicyTagVO> policyTagVOList = new ArrayList<>();

            for (PolicyTag policyTag : policyTagList) {
                PolicyTagVO policyTagVO = new PolicyTagVO();
                BeanUtils.copyProperties(policyTag, policyTagVO);
                PolicyItem policyItem = new PolicyItem();
                policyItem.setParentId(0L);
                policyTagVO.setItems(getChild(0L, policyTag.getId()));
                policyTagVOList.add(policyTagVO);
            }
            object.put("policyTag", policyTagVOList);
            return R.data(object);
        }

    }

    private List<PolicyItemVO> getChild(Long parentId, Long policyTagId) {
        PolicyItem policyItem = new PolicyItem();
        policyItem.setParentId(parentId);
        policyItem.setPolicyTagId(policyTagId);
        List<PolicyItem> policyItems = policyItemService.selectByParentId(policyItem);
        List<PolicyItemVO> policyItemVOList = new ArrayList<>();
        for (PolicyItem p : policyItems) {
            PolicyItemVO policyItemVO = new PolicyItemVO();
            BeanUtils.copyProperties(p, policyItemVO);
            policyItemVO.setItems(getChild(p.getId(), policyTagId));
            policyItemVOList.add(policyItemVO);
        }
        return policyItemVOList;
    }

    /**
     * @param policy
     * @return org.springblade.core.tool.api.R
     * @Description 修改报文基本信息
     * @Author zhangzexu
     * @Date 14:39 2020/10/09
     **/
    @PostMapping("/updatePolicy")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "修改报文基本信息", notes = "传入policy")
    @Log(title = "修改报文基本信息", businessType = BusinessType.UPDATE)
    @ApiLog("修改报文基本信息")
    public R save(@RequestBody Policy policy) {
        return R.status(policyService.updateById(policy));
    }


    /**
     * @param policyTag
     * @return org.springblade.core.tool.api.R
     * @Description 新增报文策略标签
     * @Author zhangzexu
     * @Date 14:39 2020/10/09
     **/
    @PostMapping("/addPolicyTag")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增报文策略标签", notes = "传入policyTag")
    @Log(title = "新增报文策略标签", businessType = BusinessType.INSERT)
    @ApiLog("新增报文策略标签")
    public R addPolicyTag(@RequestBody PolicyTag policyTag) {

        List<PolicyTag> policyTagList = policyTagService.list(new QueryWrapper<PolicyTag>().eq("POLICY_ID", policyTag.getPolicyId()).eq("package_tacitly", 1).eq("an_in_type", policyTag.getAnInType()));
        if (policyTagList.size() > 0 && policyTag.getPackageTacitly() == 1) {
            return R.fail("默认匹配条件已添加");
        }
        return R.status(policyTagService.saveOrUpdate(policyTag));
    }

    /**
     * @param policyItemId
     * @return org.springblade.core.tool.api.R
     * @Description 删除报文策略标签
     * @Author zhangxin
     * @Date 14:39 2020/10/09
     **/
    @GetMapping("/delPolicyItem")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "删除报文策略标签", notes = "policyItemId")
    @Log(title = "删除报文策略标签", businessType = BusinessType.DELETE)
    @ApiLog("删除报文策略标签")
    public R delPolicyItem(@RequestParam Long policyItemId) {
        PolicyItem policyItem = policyItemService.getById(policyItemId);
        if (!policyItemService.removeById(policyItemId)) {
            return R.status(false);
        }
        List<PolicyItem> policyItemList = policyItemService.selectByParentId(policyItem);
        for (int i = 0; i < policyItemList.size(); i++) {
            PolicyItem pi = policyItemList.get(i);
            pi.setOrderField(i);
            policyItemService.saveOrUpdate(pi);
        }
        return R.status(true);
    }

    /**
     * @param policyTagId
     * @return org.springblade.core.tool.api.R
     * @Description 删除报文策略标签
     * @Author zhangzexu
     * @Date 14:39 2020/10/09
     **/
    @GetMapping("/delPolicyTag")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "删除报文策略标签", notes = "传入policyTagId")
    @Log(title = "删除报文策略标签", businessType = BusinessType.DELETE)
    @ApiLog("删除报文策略标签")
    public R delPolicyTag(@RequestParam Long policyTagId) {
        boolean del = policyTagService.removeById(policyTagId);
        if (del) {
            try {
                policyItemService.deletePolicyItemTagId(policyTagId);
            } catch (Exception e) {
                log.info("删除子元素失败");
            }
        }
        return R.status(del);
    }


    /**
     * @param apiId
     * @return org.springblade.core.tool.api.R
     * @Description 删除报文策略标签
     * @Author zhangzexu
     * @Date 09:39 2020/11/13
     **/
    @GetMapping("/clearAll")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "删除api对应的所有报文策略标签", notes = "传入apiId")
    @Log(title = "删除api对应的所有报文策略标签", businessType = BusinessType.DELETE)
    @ApiLog("删除api对应的所有报文策略标签")
    public R clearAll(@RequestParam Long apiId) {
        Policy policy = policyService.getOne(new QueryWrapper<Policy>().eq("api_id", apiId));
        if (policy == null) {
            return R.fail("没有可用的报文配置");
        } else {

            List<PolicyTag> policyTagList = policyTagService.list(new QueryWrapper<PolicyTag>().eq("POLICY_ID", policy.getId()));
            for (PolicyTag policyTag : policyTagList) {
                if (!delPolicyTag(policyTag.getId()).isSuccess()) {
                    return R.status(false);
                }
            }
        }
        return R.success("删除成功");
    }


    @PostMapping("/createPolicyItem")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "新增", notes = "传入policyItem")
    @Log(title = "新增 报文策略详情表", businessType = BusinessType.INSERT)
    @ApiLog("新增 报文策略详情表")
    @Transactional(rollbackFor = Exception.class)
    public R policyItemSave(@Valid @RequestBody PolicyItem policyItem) {

        if (policyItem.getPolicyTagId() == null || policyItem.getPolicyTagId() <= 0) {
            return R.fail("请传入PolicyTagId [报文策略主键]");
        }
        List<PolicyItem> policyItemList = policyItemService.selectByParentId(policyItem);
        policyItemList.add(policyItem.getOrderField(), policyItem);
        for (int i = 0; i < policyItemList.size(); i++) {
            PolicyItem pi = policyItemList.get(i);
            pi.setOrderField(i);
            policyItemService.saveOrUpdate(pi);
        }
        return R.data(policyItemService.selectByParentId(policyItem).get(policyItem.getOrderField()));
    }

    @PostMapping("/updatePolicyItem")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入policyItem")
    @Log(title = "修改 报文策略详情表", businessType = BusinessType.UPDATE)
    @ApiLog("修改 报文策略详情表")
    @Transactional(rollbackFor = Exception.class)
    public R updatePolicyItem(@RequestBody PolicyItem policyItem) {
        if (policyItem.getPolicyTagId() <= 0) {
            return R.fail("请传入PolicyTagId [报文策略主键]");
        }
        List<PolicyItem> policyItemList = policyItemService.selectByParentId(policyItem);

        if (policyItem.getOrderField() < 0) {
            return R.fail("该字段已是第一个字段,不能再进行上移");
        }
        if (policyItem.getOrderField() >= policyItemList.size()) {
            return R.fail("该字段已是最后一个字段,不能再进行下移");
        }

        int flag = 0;
        for (int i = 0; i < policyItemList.size(); i++) {
            if (policyItem.getId().equals(policyItemList.get(i).getId())) {
                flag = i;
                break;
            }
        }
        policyItemList.remove(flag);
        policyItemList.add(policyItem.getOrderField(), policyItem);
        for (int i = 0; i < policyItemList.size(); i++) {
            PolicyItem pi = policyItemList.get(i);
            pi.setOrderField(i);
            policyItemService.saveOrUpdate(pi);
        }
        return R.success("修改成功");
    }


    @PostMapping("/dependenceMapping")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "添加映射值", notes = "传入policyItem")
    @Log(title = "添加映射值 报文策略详情表", businessType = BusinessType.INSERT)
    @ApiLog("添加映射值 报文策略详情表")
    public R dependenceMapping(@RequestBody JSONObject data) {
        Long policyItemId = data.getLong("policyItemId");
        Long policyTagId = data.getLong("policyTagId");
        Long id = data.getLong("id");
        Long apiMetadataId = data.getLong("apiMetadataId");
        Long apiId = data.getLong("apiId");

        PolicyItemMetadata policyItemMetadata = new PolicyItemMetadata();
        policyItemMetadata.setApiMetadataId(apiMetadataId);
        policyItemMetadata.setId(id);
        policyItemMetadata.setApiId(apiId);
        policyItemMetadata.setPolicyItemId(policyItemId);
        policyItemMetadata.setPolicyTagId(policyTagId);


        try {
            if (id == null || id <= 0L) {
                policyItemMetadataService.remove(new QueryWrapper<PolicyItemMetadata>().eq("policy_item_id", policyItemId));
            }
            policyItemMetadataService.saveOrUpdate(policyItemMetadata);
        } catch (Exception e) {
            return R.fail("添加映射值出错" + e.getMessage());
        }
        return R.success("添加成功");
    }

    @PostMapping("/selectMapping")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "查询映射关系", notes = "传入policyTagId")
    @Log(title = "查询映射关系", businessType = BusinessType.INSERT)
    @ApiLog("查询映射关系")
    public R selectMapping(@RequestBody JSONObject data) {
        Long policyTagId = data.getLong("policyTagId");
        try {
            return R.data(policyItemMetadataService.selectByPolicyTagId(policyTagId));
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("查询映射值出错" + e.getMessage());
        }

    }

    @PostMapping("/deleteMapping")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "清除映射值", notes = "传入policyItem")
    @Log(title = "清除映射值 报文策略详情表", businessType = BusinessType.INSERT)
    @ApiLog("清除映射值 报文策略详情表")
    public R deleteDependenceMapping(@RequestBody JSONObject data) {
        Long id = data.getLong("id");
        try {
            policyItemMetadataService.removeById(id);
        } catch (Exception e) {
            return R.fail("清除映射值出错" + e.getMessage());
        }
        return R.success("清除成功");
    }


    @PostMapping("/createJsonConfiguration")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "拆组包配置文件", notes = "传入api id ,和渠道Id")
    @Log(title = "根据api id  和渠道Id,定制化生成拆组包配置文件", businessType = BusinessType.SELECT)
    @ApiLog("根据api id  和渠道Id,定制化生成拆组包配置文件")
    public R createJsonConfiguration(@RequestBody JSONObject data) {
        Long policyId = data.getLong("policyId");
        String channelId = data.getString("channelId");
        channelId = "channelId";
        if (null == policyId) {
            return R.data(null);
        }
        Policy policy = policyService.getById(policyId);
        if (policy == null) {
            return R.data(null);
        }
        policy.setPolicyType(1);
        Long sysId = apiMetadataService.findSysIdByApiId(policy.getApiId());
        ApiMetadataVO apiMetadataVO = new ApiMetadataVO();
        apiMetadataVO.setSysId(sysId);
        List<ApiMetadataVO> sysTotalList = apiMetadataService.selectApiMetadataList(apiMetadataVO);

        ApiMetadataVO reqRoot = new ApiMetadataVO();


        ApiMetadataVO resRoot = new ApiMetadataVO();


        List<ApiMetadata> apiMetadataListReq = apiMetadataService.list(new QueryWrapper<ApiMetadata>().eq("api_id", policy.getApiId()).eq("body_type", "1").eq("in_type", "body").orderByAsc("order_field"));
        List<ApiMetadata> apiMetadataListRes = apiMetadataService.list(new QueryWrapper<ApiMetadata>().eq("api_id", policy.getApiId()).eq("body_type", "2").eq("in_type", "body").orderByAsc("order_field"));


        for (ApiMetadataVO a : sysTotalList) {
            if (a.getGroupId() == 0 && a.getBodyType() == 1) {
                reqRoot = a;
            } else if (a.getGroupId() == 0 && a.getBodyType() == 2) {
                resRoot = a;
            } else {
                ApiMetadata apiMetadata = new ApiMetadata();
                BeanUtils.copyProperties(a, apiMetadata);
                if (a.getBodyType() == 1) {
                    apiMetadataListReq.add(apiMetadata);
                } else {
                    apiMetadataListRes.add(apiMetadata);
                }
            }
        }
        //对请求参数进行组包,找出groupId = 0 的,开始获取 jpath

        HashMap<Long, String> mapReq = new HashMap<>();
        metadataJPath(apiMetadataListReq, mapReq, reqRoot.getId(), "");

        //apiMetadataListReq 结束
        //响应数据处理 拆包
        List<PolicyTag> policyTagList = policyTagService.list(new QueryWrapper<PolicyTag>().eq("POLICY_ID", policy.getId()));
        HashMap<Long, String> mapRes = new HashMap<>();
        for (PolicyTag policyTag : policyTagList) {
            if (policyTag.getAnInType() == 2) {
                if (policyTag.getTagType() == 3) {
                    List<PolicyItem> policyItemList = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                    for (PolicyItem pi : policyItemList) {
                        if (pi.getParentId() == 0) {
                            policyItemJPath(policyItemList, mapRes, 0L, "", policyTag.getId());
                        }
                    }
                }
            }
        }


        //映射关系转化为hash map ;
        List<PolicyItemMetadata> policyItemMetadatas = policyItemMetadataService.list(new QueryWrapper<PolicyItemMetadata>().eq("api_id", policy.getApiId()));
        HashMap<Long, String> policyItemMetadataMap = new HashMap<>();

        HashMap<String, String> metadataPolicyItemMap = new HashMap<>();


        for (PolicyItemMetadata policyItemMetadata : policyItemMetadatas) {
            policyItemMetadataMap.put(policyItemMetadata.getPolicyItemId(), mapReq.get(policyItemMetadata.getApiMetadataId()));

        }

        for (PolicyItemMetadata policyItemMetadata : policyItemMetadatas) {
            String value = mapRes.get(policyItemMetadata.getPolicyItemId());
            if (!StringUtils.isEmpty(value)) {
                String[] values = value.split("-");
                metadataPolicyItemMap.put(values[0] + "-" + policyItemMetadata.getApiMetadataId(), values[1]);
            }
        }


        List<PolicyItem> packageIntegrate = null;
        HashMap<String, String> packageCheckIntegrateHashMap = new HashMap<>();
        HashMap<String, PolicyTag> packageCheckIntegrateTag = new HashMap<>();

        HashMap<String, String> packageCheckAnalyzeHashMap = new HashMap<>();
        HashMap<String, PolicyTag> packageCheckAnalyzeTag = new HashMap<>();

        HashMap<String, Object> root = new HashMap<>();

        for (PolicyTag policyTag : policyTagList) {
            if (policyTag.getAnInType() == 1) {
                if (policyTag.getTagType() == 1) {
                } else if (policyTag.getTagType() == 4) {
                } else if (policyTag.getTagType() == 2) {
                } else if (policyTag.getTagType() == 3 && StringUtils.isEmpty(policyTag.getPackageCheck()) && StringUtils.isEmpty(policyTag.getPackageName())) {
                    packageIntegrate = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                    for (PolicyItem policyItem : packageIntegrate) {
                        policyItem.setJname(policyItemMetadataMap.get(policyItem.getId()));
                    }
                } else {
                    List<PolicyItem> packageCheckIntegrateList = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                    for (PolicyItem policyItem : packageCheckIntegrateList) {
                        policyItem.setJname(policyItemMetadataMap.get(policyItem.getId()));
                    }
                    StringBuilder stringBuilder = createItemXml(packageCheckIntegrateList, 0L, policy.getPolicyType(), policyTag.getTagType());
                    packageCheckIntegrateTag.put(policyTag.getId() + "", policyTag);
                    packageCheckIntegrateHashMap.put(policyTag.getId() + "", stringBuilder == null ? "" : stringBuilder.toString());
                }
            } else {
                if (policyTag.getTagType() == 1) {
                } else if (policyTag.getTagType() == 4) {
                } else if (policyTag.getTagType() == 2) {
                } else if (policyTag.getTagType() == 3 && StringUtils.isEmpty(policyTag.getPackageCheck()) && StringUtils.isEmpty(policyTag.getPackageName())) {
                    root.put("packageAnalyze", createItemXml(apiMetadataListRes, resRoot.getId(), metadataPolicyItemMap, policyTag.getId()));
                } else {
                    StringBuilder stringBuilder = createItemXml(apiMetadataListRes, resRoot.getId(), metadataPolicyItemMap, policyTag.getId());
                    packageCheckAnalyzeTag.put(policyTag.getId() + "", policyTag);
                    packageCheckAnalyzeHashMap.put(policyTag.getId() + "", stringBuilder == null ? "" : stringBuilder.toString());

                }
            }
        }

        root.put("policy", policy);
        root.put("packageIntegrate", createItemXml(packageIntegrate, 0L, policy.getPolicyType(), 3));
        root.put("packageCheckIntegrate", packageCheckIntegrateHashMap);
        root.put("packageCheckIntegrateTag", packageCheckIntegrateTag);


        root.put("packageCheckAnalyze", packageCheckAnalyzeHashMap);
        root.put("packageCheckAnalyzeTag", packageCheckAnalyzeTag);

        ApiListVo apiVO = apiService.selectApiVoById(policy.getApiId());


        policy.setFileUrl(fileUploadPath);
//        policy.setFileName(apiVO.getApiCode() + "-" + channelId + "-backservice.xml");
        policy.setFileName(apiVO.getApiCode() + "-" + DateUtils.getDateTimes() + "-backservice.xml");

        String filePath = policy.getFileUrl() + File.separator + apiVO.getApiCode() + File.separator + policy.getFileName().replace("-backservice.xml", "-gateway.xml");

        TemplateConfig.generate(filePath, root, "/json/policyJsonToJson.ftl");

        StringBuilder string = new StringBuilder();
        //xml 格式化
        try {
            try {
                FileReader reader = new FileReader(filePath);
                BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
                String line;
                //网友推荐更加简洁的写法
                while ((line = br.readLine()) != null) {
                    // 一次读入一行数据
                    string.append(line);
                }
            } catch (IOException e) {
                log.error("文件读取失败", e);
            }

            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
            randomAccessFile.close();
            randomAccessFile = new RandomAccessFile(filePath, "rw");
            randomAccessFile.seek(0);
            String msg = XmlUtils.format(string.toString());
            randomAccessFile.write(msg.getBytes("GBK"));
            return R.data(msg);
        } catch (Exception e) {
            log.error("生成报文拆组包失败", e);
            return R.fail(e.getLocalizedMessage());
        }

    }

    public void metadataJPath(List<ApiMetadata> apiMetadataListReq, HashMap<Long, String> hashMap, Long groupId, String keys) {
        for (ApiMetadata a : apiMetadataListReq) {
            if (a.getGroupId().equals(groupId)) {
                String key = keys == "" ? "" + a.getName() : keys + "." + a.getName();
                hashMap.put(a.getId(), key);
                metadataJPath(apiMetadataListReq, hashMap, a.getId(), key);
            }
        }
    }

    public void policyItemJPath(List<PolicyItem> policyItemList, HashMap<Long, String> hashMap, Long groupId, String keys, Long policyTagId) {
        for (PolicyItem pi : policyItemList) {
            if (pi.getParentId().equals(groupId)) {
                String key = keys == "" ? "" + pi.getName() : keys + "." + pi.getName();
                hashMap.put(pi.getId(), policyTagId + "-" + key);
                policyItemJPath(policyItemList, hashMap, pi.getId(), key, policyTagId);
            }
        }
    }

    private StringBuilder createItemXml(List<ApiMetadata> apiMetadataList, long parentId, HashMap<String, String> metadataPolicyItemMap, Long policyTagId) {
        StringBuilder item = new StringBuilder("");
        if (apiMetadataList == null || apiMetadataList.size() <= 0) {
            return null;
        }

        for (ApiMetadata apiMetadata : apiMetadataList) {
            if (apiMetadata.getGroupId() == parentId) {
                item.append("<ITEM");
                StringBuilder childItem = createItemXml(apiMetadataList, apiMetadata.getId(), metadataPolicyItemMap, policyTagId);
                item.append(apiMetadata.toXml(metadataPolicyItemMap.get(policyTagId + "-" + apiMetadata.getId())));
                if (childItem.length() <= 0) {
                    item.append("/>\n");
                } else {
                    item.append(">\n");
                    if ("object".equals(apiMetadata.getTypeFormat())) {
                        item.append("<ITEM type=\"Object\">");
                        item.append(childItem);
                        item.append("</ITEM>");
                    } else {
                        item.append(childItem);
                    }
                    item.append("</ITEM>\n");
                }
            }
        }
        return item;
    }


    @PostMapping("/createPolicyConfiguration")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "拆组包配置文件", notes = "传入api id ,和渠道Id")
    @Log(title = "根据api id  和渠道Id,定制化生成拆组包配置文件", businessType = BusinessType.SELECT)
    @ApiLog("根据api id  和渠道Id,定制化生成拆组包配置文件")
    public R createPolicyConfiguration(@RequestBody JSONObject data) {
        String channelId = data.getString("channelId");
        channelId = "channelId";
        Long policyId = data.getLong("policyId");
        if (policyId == null) {
            return R.data(null);
        }
        Policy policy = policyService.getById(policyId);
        if (policy == null) {
            return R.data(null);
        }
        List<PolicyTag> policyTagList = policyTagService.list(new QueryWrapper<PolicyTag>().eq("POLICY_ID", policy.getId()));

        List<PolicyItem> firstworkAnalyze = null;
        List<PolicyItem> lastworkAnalyze = null;
        List<PolicyItem> packageAnalyze = null;
        List<PolicyItem> headAnalyze = null;
        HashMap<String, String> packageCheckAnalyzeHashMap = new HashMap<>();
        HashMap<String, PolicyTag> packageCheckAnalyzeTag = new HashMap<>();

        List<PolicyItem> firstworkIntegrate = null;
        List<PolicyItem> lastworkIntegrate = null;
        List<PolicyItem> packageIntegrate = null;
        List<PolicyItem> headIntegrate = null;

        HashMap<String, String> packageCheckIntegrateHashMap = new HashMap<>();
        HashMap<String, PolicyTag> packageCheckIntegrateTag = new HashMap<>();

        for (PolicyTag policyTag : policyTagList) {
            if (policyTag.getAnInType() == 1) {
                if (policyTag.getTagType() == 1) {
                    firstworkIntegrate = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else if (policyTag.getTagType() == 4) {
                    lastworkIntegrate = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else if (policyTag.getTagType() == 3 && StringUtils.isEmpty(policyTag.getPackageCheck()) && StringUtils.isEmpty(policyTag.getPackageName())) {
                    packageIntegrate = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else if (policyTag.getTagType() == 2) {
                    headIntegrate = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else {

                    StringBuilder stringBuilder = createItemXml(policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field")), 0L, policy.getPolicyType(), policyTag.getTagType());
                    packageCheckIntegrateTag.put(policyTag.getId() + "", policyTag);
                    packageCheckIntegrateHashMap.put(policyTag.getId() + "", stringBuilder == null ? "" : stringBuilder.toString());
                }

                // 组包 Integrate
            } else {
                // 拆包 Analyze
                if (policyTag.getTagType() == 1) {
                    firstworkAnalyze = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else if (policyTag.getTagType() == 4) {
                    lastworkAnalyze = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else if (policyTag.getTagType() == 3 && StringUtils.isEmpty(policyTag.getPackageCheck()) && StringUtils.isEmpty(policyTag.getPackageName())) {
                    packageAnalyze = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else if (policyTag.getTagType() == 2) {
                    headAnalyze = policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field"));
                } else {
                    StringBuilder stringBuilder = createItemXml(policyItemService.list(new QueryWrapper<PolicyItem>().eq("POLICY_TAG_ID", policyTag.getId()).orderByAsc("order_field")), 0L, policy.getPolicyType(), policyTag.getTagType());
                    packageCheckAnalyzeTag.put(policyTag.getId() + "", policyTag);
                    packageCheckAnalyzeHashMap.put(policyTag.getId() + "", stringBuilder == null ? "" : stringBuilder.toString());
                }
            }
        }

        HashMap<String, Object> root = new HashMap<>();
        root.put("policy", policy);
        root.put("policyTagList", policyTagList);

        root.put("headAnalyze", createItemXml(headAnalyze, 0L, policy.getPolicyType(), 2));
        root.put("firstworkAnalyze", createItemXml(firstworkAnalyze, 0L, policy.getPolicyType(), 1));
        root.put("lastworkAnalyze", createItemXml(lastworkAnalyze, 0L, policy.getPolicyType(), 4));
        root.put("packageAnalyze", createItemXml(packageAnalyze, 0L, policy.getPolicyType(), 3));
        root.put("packageCheckAnalyze", packageCheckAnalyzeHashMap);
        root.put("packageCheckAnalyzeTag", packageCheckAnalyzeTag);

        root.put("headIntegrate", createItemXml(headIntegrate, 0L, policy.getPolicyType(), 2));
        root.put("firstworkIntegrate", createItemXml(firstworkIntegrate, 0L, policy.getPolicyType(), 1));
        root.put("lastworkIntegrate", createItemXml(lastworkIntegrate, 0L, policy.getPolicyType(), 4));
        root.put("packageIntegrate", createItemXml(packageIntegrate, 0L, policy.getPolicyType(), 3));
        root.put("packageCheckIntegrate", packageCheckIntegrateHashMap);
        root.put("packageCheckIntegrateTag", packageCheckIntegrateTag);

        ApiListVo apiVO = apiService.selectApiVoById(policy.getApiId());

        policy.setFileUrl(fileUploadPath);
//        policy.setFileName(apiVO.getApiCode() + "-" + channelId + "-backservice.xml");
        policy.setFileName(apiVO.getApiCode() + "-" + DateUtils.getDateTimes() + "-backservice.xml");

        policyService.saveOrUpdate(policy);


        String filePath = policy.getFileUrl() + File.separator + apiVO.getApiCode() + File.separator + policy.getFileName();

        TemplateConfig.generate(policy.getFileUrl() + File.separator + apiVO.getApiCode() + File.separator + policy.getFileName(), root, "/json/policyXml.ftl");
        StringBuilder string = new StringBuilder();
        //xml 格式化
        try {
            try {
                FileReader reader = new FileReader(filePath);
                BufferedReader br = new BufferedReader(reader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
                String line;
                //网友推荐更加简洁的写法
                while ((line = br.readLine()) != null) {
                    // 一次读入一行数据
                    string.append(line);
                }
            } catch (IOException e) {
                log.error("文件读取失败", e);
            }

            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
            RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
            randomAccessFile.close();
            randomAccessFile = new RandomAccessFile(filePath, "rw");
            randomAccessFile.seek(0);
            String msg = XmlUtils.format(string.toString());
            randomAccessFile.write(msg.getBytes("GBK"));

            randomAccessFile.close();

            com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(policy.getApiId());

            Plupload plupload = null;
            if (api.getMsgConfigId() != null && !"".equals(api.getMsgConfigId())) {
                plupload = pluploadService.getById(api.getMsgConfigId());
            }
            if (plupload == null) {
                plupload = new Plupload();
            }

            plupload.setFileFullpath(filePath.replace(policy.getFileUrl(), "/file"));
            plupload.setFileRealname(policy.getFileName());
            plupload.setStorageName(policy.getFileName());
            plupload.setStorageType(1L);

            pluploadService.saveOrUpdate(plupload);
            api.setMsgConfigId(plupload.getId());
            api.setBackserviceReqType(policy.getPolicyType() + "");
            api.setIsUploadBackFile("2");
            apiService.updateById(api);
            return R.data(msg);
        } catch (Exception e) {
            log.error("生成报文拆组包失败", e);
            return R.fail(e.getLocalizedMessage());
        }

    }

    @PostMapping("/testPolicyConfiguration")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "测试拆组包配置文件", notes = "传入api id ,和渠道Id,拆组包类型 ")
    @Log(title = "根据api id  和渠道Id,定制化生成拆组包配置文件", businessType = BusinessType.SELECT)
    @ApiLog("根据api id  和渠道Id,测试生成拆组包配置文件是否正确")
    public R testPolicyConfiguration(@RequestBody JSONObject data) {
        Long policyId = data.getLong("policyId");
        String message = data.getString("message");
        String msg = "";
        try {
            msg = URLDecoder.decode(message, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            log.error("异常信息:{}", e);
        }
        int anInType = data.getInteger("anInType");

        try {
            String result = "";
            Policy policy = null;
            if (policyId != null) {
                policy = policyService.getById(policyId);
            }
            int type = 2;
            Long apiId = 0L;
            String fileName = "";
            if (policy == null) {
                type = data.getInteger("policyType");
                apiId = data.getLong("apiId");
            } else {
                type = policy.getPolicyType();
                apiId = policy.getApiId();
                fileName = policy.getFileName();

            }
            String policyType = "XML";
            switch (type) {
                //JSON
                case 1:
                    policyType = "JSON";
                    break;
                //XML
                case 2:
                    policyType = "XML";
                    break;
                //分隔
                case 3:
                    policyType = "SEPARATOR";
                    break;
                //定长
                case 4:
                    policyType = "FIEXD";
                    break;
                //8583
                case 5:
                    policyType = "EFET";
                    break;
                default:
                    break;
            }
            ApiListVo apiVO = apiService.selectApiVoById(apiId);
            String path = "";
            if ("".equals(fileName)) {
                com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(apiId);
                if (api != null) {
                    Plupload plupload = pluploadService.getById(api.getMsgConfigId());
                    if (plupload != null) {
                        fileName = plupload.getStorageName();
                        path = fileServerPath;
                    }
                }
            } else {
                path = fileServerPath + "/" + apiVO.getApiCode() + "/";
            }
            log.info("报文配置路径：{}", path);
            MessageMain.registerPolicy(path, Arrays.asList(fileName));
//            MessageMain.registerPolicy("http://114.116.27.20:9003/file/", Arrays.asList("amq009.xml"));
            if (anInType == 2) {
                result = MessageMain.conversionMessageWithEx(ConversionType.valueOf("Analyze"), MessageType.valueOf(policyType), fileName, msg);
            } else {
                result = MessageMain.conversionMessageWithEx(ConversionType.valueOf("Integrate"), MessageType.valueOf(policyType), fileName, msg);
            }
            try {
                if ("XML".equals(policyType) && anInType == 1) {
                    return R.data(XmlUtils.format(result));
                } else if ("JSON".equals(policyType) || anInType == 2) {
                    return R.data(JSON.toJSONString(JSONObject.parseObject(result), SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat));
                }
            } catch (Exception e) {
                log.error("异常信息:{}", e);
            }
            return R.data(result);
        } catch (Exception e) {
            log.error("拆组包测试,出现问题", e);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            e.printStackTrace(new java.io.PrintWriter(buf, true));
            String expMessage = buf.toString();
            try {
                buf.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
            return R.data(expMessage);
        }


    }


    private StringBuilder createItemXml(List<PolicyItem> policyItems, long parentId, int policyType, int tagType) {
        StringBuilder item = new StringBuilder("");
        if (policyItems == null || policyItems.size() <= 0) {
            return null;
        }

        for (PolicyItem policyItem : policyItems) {
            if (policyItem.getParentId() == parentId) {
                item.append("<ITEM");
                StringBuilder childItem = createItemXml(policyItems, policyItem.getId(), policyType, tagType);
                item.append(policyItem.toString(policyType, tagType));

                if (childItem.length() <= 0) {
                    item.append("/>\n");
                } else {
                    item.append(">\n");
                    if (policyItem.getStruct() == 1) {
                        item.append("<SUB>");
                        item.append(childItem);
                        item.append("</SUB>");
                    } else if (policyItem.getDataType() == 7) {
                        item.append("<ITEM type=\"Object\">");
                        item.append(childItem);
                        item.append("</ITEM>");
                    } else {
                        item.append(childItem);
                    }
                    item.append("</ITEM>\n");
                }
            }
        }
        return item;
    }


    @ApiLog("修改报文配置文件")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "修改报文配置文件", notes = "修改报文配置文件")
    @Log(title = "修改报文配置文件", businessType = BusinessType.UPDATE)
    @PostMapping("/editPolicyXml")
    public R editPolicyXml(@Valid @RequestBody JSONObject jsonObject) {
        Long id = jsonObject.getLong("apiId");
        String type = jsonObject.getString("type");
        String msgType = jsonObject.getString("msgType");
        String content = jsonObject.getString("content");
        if(id == null || "".equals(content)){
            return R.fail("apiId和拆组包文件内容不能为空！");
        }
        com.dhcc.bpm.modules.api.entity.Api api = apiService.getById(id);
        if ("1".equals(type)) {
            //客户端报文修改

        } else {
            //服务端报文修改
            Plupload plupload = pluploadService.getById(api.getMsgConfigId());
            if (plupload != null) {
                String path =  plupload.getFileFullpath().replace("/file", fileUploadPath);
                log.info("配置文件路径：{}", path);

                saveApiOpeLog(ReadTextFile.readfile(path), content, api.getApiVersion(), id);
                ReadTextFile.writefile(path, content, false);
            }
        }
        return R.status(true);
    }

    public void saveApiOpeLog(String oldParam, String newPaam, String version, Long apiId){
        //接口修改操作记录
        ApiOperLog apiOperLog = new ApiOperLog();
        apiOperLog.setBeforeParam(oldParam);
        apiOperLog.setOperParam(newPaam);
        apiOperLog.setVersion(version);
        apiOperLog.setApiId(apiId);
        apiOperLogService.save(apiOperLog);
    }
}
